package com.jinlejia.erp.ui.mine.transfer;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityDetailTransferBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.DateUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Map;

import okhttp3.Call;

/**
 * 异动详情
 */
public class DetailTransferActivity extends AppCompatActivity {

    String userbdCode;
    String infoStr;

    private ActivityDetailTransferBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_detail_transfer);
        binding.setEvent(this);
        if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
            Uri uri = getIntent().getData();
            if (uri != null) {
                userbdCode = uri.getQueryParameter("code");
            }
            if (uri.getPath().equals("/userbdauditdetails")) {
                binding.auditBtn.setVisibility(View.VISIBLE);
            } else {
                binding.auditBtn.setVisibility(View.GONE);
            }
        } else {
            if (!getIntent().getBooleanExtra("isAudit", true))
                binding.auditBtn.setVisibility(View.GONE);

            userbdCode = getIntent().getStringExtra("userbdCode");
        }

        getData();

    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.audit_btn:
                Intent aIntent = new Intent(DetailTransferActivity.this, TransferAuditActivity.class);
                aIntent.putExtra("userbdCode", userbdCode);
                startActivity(aIntent);
                break;
            case R.id.transfer_infomation:
                Intent intent = new Intent(DetailTransferActivity.this, TransferInfomationActivity.class);
                intent.putExtra("info", infoStr);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void getData() {
        OkHttpUtils.get()
                .url(ApiConfig.TRANSFER_DETAIL)
                .addParams("userbdCode", userbdCode)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(DetailTransferActivity.this, response)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            infoStr = data.toJSONString();
                            //异动人
                            binding.transferMan.setText(data.getString("userName"));
                            //异动类型
                            binding.transferType.setText(data.getString("userbdType"));
                            //异动原因
                            binding.transferReason.setText(data.getString("remark"));
                            //异动时间
                            binding.transferTime.setText(DateUtil.getTime(data.getLongValue("applyTime"), "yyyy-MM-dd HH:mm:ss"));

                            //审核信息
                            if (!data.containsKey("reviewUserbd") || data.getJSONArray("reviewUserbd").isEmpty()) {
                                binding.layoutAuditInfo.setVisibility(View.GONE);
                            }
                            JSONArray auditInfo = data.getJSONArray("reviewUserbd");
                            for (int i = 0; i < auditInfo.size(); i++) {
                                JSONObject object = (JSONObject) auditInfo.get(i);
                                if (object.get("reviewType").toString().equals("0")) {
                                    //原上级
                                    binding.oldSupervisorLayout.setVisibility(View.VISIBLE);
                                    //原上级审核时间
                                    binding.oldSupervisorTime.setText(DateUtil.getTime(object.getLongValue("reviewTime"), "yyyy-MM-dd HH:mm:ss"));
                                    //原上级名
                                    binding.oldSupervisorName.setText(object.getString("reviewName"));
                                    //审核状态
                                    switch (object.getIntValue("reviewState")) {
                                        case 0:
                                            binding.oldSupervisorAuditState.setText("待审核");
                                            binding.newSupervisorAuditState.setTextColor(Color.parseColor("#FF0000"));
                                            binding.oldSupervisorTime.setVisibility(View.GONE);
                                            binding.timeIcon.setVisibility(View.GONE);
                                            break;
                                        case 1:
                                            binding.oldSupervisorAuditState.setText("审核不通过");
                                            binding.newSupervisorAuditState.setTextColor(Color.parseColor("#FF0000"));
                                            break;
                                        case 2:
                                            binding.oldSupervisorAuditState.setText("审核通过");
                                            binding.newSupervisorAuditState.setTextColor(Color.parseColor("#0B9800"));
                                            break;
                                    }
                                    //审核不通过原因
                                    if (object.containsKey("rejectReason") && !TextUtils.isEmpty(object.getString("rejectReason"))) {
                                        binding.oldReason.setText("驳回原因：".concat(object.getString("rejectReason")));
                                    }

                                } else {
                                    //新上级
                                    binding.newSupervisorLayout.setVisibility(View.VISIBLE);
                                    //新上级审核时间
                                    binding.newSupervisorTime.setText(DateUtil.getTime(object.getLongValue("reviewTime"), "yyyy-MM-dd HH:mm:ss"));
                                    //新上级名
                                    binding.newSupervisorName.setText(object.getString("reviewName"));
                                    //审核状态
                                    switch (object.getIntValue("reviewState")) {
                                        case 0:
                                            binding.newSupervisorAuditState.setText("待审核");
                                            binding.newSupervisorAuditState.setTextColor(Color.parseColor("#FF0000"));
                                            binding.newTimeIcon.setVisibility(View.GONE);
                                            binding.newSupervisorTime.setVisibility(View.GONE);
                                            break;
                                        case 1:
                                            binding.newSupervisorAuditState.setText("审核不通过");
                                            binding.newSupervisorAuditState.setTextColor(Color.parseColor("#FF0000"));
                                            break;
                                        case 2:
                                            binding.newSupervisorAuditState.setText("审核通过");
                                            binding.newSupervisorAuditState.setTextColor(Color.parseColor("#0B9800"));
                                            break;
                                    }
                                    //审核不通过原因
                                    if (object.containsKey("rejectReason") && !TextUtils.isEmpty(object.getString("rejectReason"))) {
                                        binding.newReason.setVisibility(View.VISIBLE);
                                        binding.newReason.setText("驳回原因：".concat(object.getString("rejectReason")));
                                    }
                                }
                            }

                        }
                    }
                });
    }
}
